package com.atguigu.spzx.service.impl;

import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.atguigu.spzx.mapper.ProductDetailsMapper;
import com.atguigu.spzx.mapper.ProductMapper;
import com.atguigu.spzx.mapper.ProductSkuMapper;
import com.atguigu.spzx.model.dto.h5.ProductSkuDto;
import com.atguigu.spzx.model.dto.product.SkuSaleDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.h5.ProductItemVo;
import com.atguigu.spzx.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Override
    public List<ProductSku> selectSkuBySal() {
        return productSkuMapper.selectSkuBySal();
    }

    @Override
    public PageInfo<ProductSku> findByPage(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        PageHelper.startPage(page, limit);
        List<ProductSku> list = productSkuMapper.findByPage(productSkuDto);
        return new PageInfo<>(list);
    }

    @Override
    public ProductItemVo item(Integer skuid) {
        ProductItemVo vo = new ProductItemVo();
        ProductSku productSku = productSkuMapper.selectSkuById(skuid);
        Product product = productMapper.selectById(productSku.getProductId());
        List<ProductSku> list = productSkuMapper.selectSkuByProductId(product.getId());
        ProductDetails productDetails = productDetailsMapper.selectByProductId(product.getId());

        Map<String,Object> skuSpecValueMap = new HashMap<>();
        list.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(), item.getId());
        });

        vo.setProductSku(productSku);
        vo.setProduct(product);
        vo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
        vo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
        vo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
        vo.setSkuSpecValueMap(skuSpecValueMap);

        return vo;

    }

    @Override
    public ProductSku getBySkuId(Long skuId) {
        return productSkuMapper.selectSkuById(Math.toIntExact(skuId));
    }

    @Transactional
    @Override
    public Boolean updateSkuSaleNum(List<SkuSaleDto> skuSaleDtoList) {
        if(!CollectionUtils.isEmpty(skuSaleDtoList)) {
            for(SkuSaleDto skuSaleDto : skuSaleDtoList) {
                productSkuMapper.updateSale(skuSaleDto.getSkuId(), skuSaleDto.getNum());
            }
        }
        return true;
    }
}
